package com.aptech.restrant.Controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.aptech.restrant.Bean.UserDataBean;
import com.aptech.restrant.Model.UserBean;

public class UserLogin extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		this.doPost(request, response);
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		response.setContentType("text/html;charset=gbk");

		if(request.getParameter("out")!=null)
		{
			if(request.getParameter("out").toString().equals("exit"))
			{
				session.removeAttribute("user");
				session.removeAttribute("cart");
				session.removeAttribute("currentUrl");
				session.setAttribute("active",new String("系统注销成功！"));
				response.sendRedirect("../Active.jsp");
				return;
			}
		}
		
		String content = "";
		
		String userName = request.getParameter("loginName");
		String userPass = request.getParameter("loginPass");
		String input=request.getParameter("rand1");
		UserDataBean udb = new UserDataBean();
		UserBean user = udb.findUserByName(userName);

		if(user!=null )
		{
			if(user.getUserPass().equals(userPass))
			{
				//session.setAttribute("user", user);
				if((session.getAttribute("rand")).equals(input))
				{
					Cookie ckName = new Cookie("loginName",user.getUserName());
					ckName.setMaxAge(60*60*24*1);
			  		Cookie ckPass = new Cookie("loginPass",user.getUserPass());
			  		ckPass.setMaxAge(60*60*24*1);
			  		response.addCookie(ckName);
			  		response.addCookie(ckPass);
			  		
			  		//System.out.println(request.getParameter("logins"));
					content = "用户登陆成功";
					session.setAttribute("user",user);
					request.getSession().setAttribute("is_login", "yes");
					
				}
				else
				{
					content="验证码错误";
				}
			
			}
			
			else{
				content = "用户密码错误";		
			}
		}
		
		else{
			content = "用户不存在";
			session.setAttribute("user", user);
		}
		
		session.setAttribute("active", new String(content));
		//System.out.println("$$$$$$$$$$$$$$$$$$$$$");
		RequestDispatcher dispatcher = request.getRequestDispatcher("../Active.jsp");
		dispatcher.forward(request, response);
	}

}
